// miniprogram/pages/myBooking/myBookiing.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookingData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyAllBookingData();
  },

  // 获取用户所有记账
  getMyAllBookingData() {
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });

    wx.cloud.callFunction({
      name: 'find_all_data',
      data: {
        listName: 'booking'
      }
    }).then(res => {
      wx.hideLoading();
      console.log(res);
      this.setData({
        bookingData: res.result.data
      })
    }).catch(err => {
      wx.hideLoading();
      console.log(err);
    })
  },

  // 删除用户当前记账
  removeBooking(e) {
    console.log(e);
    
    wx.showModal({
      title: '提示',
      content: '确定删除这条记录吗',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '拼命加载中',
            mask: true
          });
      
          wx.cloud.callFunction({
            name: 'remove_data',
            data: {
              listName: 'booking',
              _id: e.detail._id
            }
          }).then(result => {
            wx.hideLoading();
            console.log(result);

            if (result.result.stats.removed == 1) {
              this.data.bookingData.splice(e.currentTarget.dataset.index, 1);
              this.setData({
                bookingData: this.data.bookingData
              });
              wx.showToast({
                title: '删除成功',
                icon: 'none',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: '删除失败',
                icon: 'none',
                duration: 2000
              })
            }

          }).catch(err => {
            wx.hideLoading();
            wx.showToast({
              title: '删除失败',
              icon: 'none',
              duration: 2000
            })
            console.log(err);
          })
        } 
      }
    })

  }
})